package com.zhongkai.app.controller;

import com.zhongkai.app.service.AppRegLogService;
import com.zhongkai.app.service.SendSmsService;
import com.zhongkai.model.utils.ZhongkaiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Controller
public class AppRegLogController {

    @Autowired
    private AppRegLogService appRegLogService;  //注册 登录

    @Autowired
    private SendSmsService sendSmsService;  //验证码发送



    /**
     * APP注册
     * @param phone 手机号
     * @param password 密码
     * @param code 验证码
     */
    @RequestMapping(value = "/app/registered", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult registered(
            String phone, String password,String code

    ) {


        ZhongkaiResult zhongkaiResult = null;
        try {
            zhongkaiResult = appRegLogService.user_registered(
                    phone
                    , password
                    , code


            );
        } catch (IOException e) {
            e.printStackTrace();

            return ZhongkaiResult.build(401, "转换失败,请联系管理员");
        }

        return zhongkaiResult;
    }



    /**
     * APP登录
     * @param phone 手机号
     * @param password 密码
     * @param code 验证码
     * @param way 登录方式 1 密码登录 2验证码登录
     */
    @RequestMapping(value = "/app/login", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult user_login(String phone, String password ,String code,String way) {
        // session.setAttribute("user", zhongkaiResult.getData());
        try {

            ZhongkaiResult zhongkaiResult = appRegLogService.user_login(phone, password,code,way);

            return zhongkaiResult;

        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(500,"登录失败");

        }

    }



    /**
     * APP发送验证码
     * @param phone 手机号
     */
    @RequestMapping(value = "/app/send_code", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult send(String phone) {
        // ZhongkaiResult zhongkaiResult = appRegLogService.get_user_all();
        ZhongkaiResult send = sendSmsService.send(phone);
        return send;
    }




    /**
     * APP根据手机号验证码修改密码
     * @param phone 手机号
     * @param password 密码
     * @param code 验证码
     */
    @RequestMapping(value = "/app/update_code_password", produces = "application/json;charset=utf-8")
    @ResponseBody
    public ZhongkaiResult getUserAll(String phone, String password,String code) {
        // ZhongkaiResult zhongkaiResult = appRegLogService.get_user_all();
        ZhongkaiResult result = appRegLogService.updatePassword(phone, password, code);
        return result;
    }


    @RequestMapping(value = "/tcp")
    @ResponseBody
    public ZhongkaiResult test(String token, HttpServletRequest req) {
/*      System.out.println("有访问到达了");
        String remoteAddr = req.getRemoteAddr();
        System.out.println(remoteAddr);*/

        try {
            appRegLogService.revision_user(token);
            return ZhongkaiResult.ok("更新成功");
        } catch (IOException e) {
            e.printStackTrace();
            return ZhongkaiResult.build(402, "更新失败");
        }
/*        String data = (String) zhongkaiResult.getData();
        System.out.println(data);
        try {
            Connect connect = new Connect();
            connect.connect();
            String send = connect.newMessage(data);
            System.out.println(send);
            if (send.equals("succeed_ok")){
                return ZhongkaiResult.ok(send);
            }else {
                return ZhongkaiResult.build(201, send);
            }

            //      connect.tRecv.start();
*//*            if (connect.newMessage(ip)) {
                return ZhongkaiResult.ok();
            }else {
                return ZhongkaiResult.build(201, "失败001");
            }*//*
         *//*            if () {
                return ZhongkaiResult.ok();
            } else {
                return ZhongkaiResult.build(201, "失败001");
            }*//*
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(201, "失败002");
        }*/




    }




}
